package com.tom.handler;

import com.alibaba.fastjson.JSON;
import com.tom.util.AjaxResult;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        // 设置响应头
        response.setHeader("content-type", "text/html;charset=UTF-8" );
        AjaxResult ajaxResult = AjaxResult.me().setSuccess(false).setMessage("认证失败[401]");
        response.getWriter().print(JSON.toJSONString(ajaxResult));
        response.getWriter().flush();
        response.getWriter().close();
    }
}